<header class="flex items-center justify-between">
  <h1 class="text-primary text-xl font-medium">Rules</h1>
  <div class="flex items-center gap-2">
    <% if @rules.any? %>
      <%= render DS::Menu.new do |menu| %>
        <% menu.with_item(
          variant: "button",
          text: "Delete all rules",
          href: destroy_all_rules_path,
          icon: "trash-2",
          method: :delete,
          confirm: CustomConfirm.for_resource_deletion("all rules", high_severity: true)) %>
      <% end %>
    <% end %>
    <%= render DS::Link.new(
      text: "New rule",
      variant: "primary",
      href: new_rule_path(resource_type: "transaction"),
      icon: "plus",
      frame: :modal
    ) %>
  </div>
</header>
<% if self_hosted? %>
  <div class="flex items-center gap-2 mb-2 py-4">
    <%= icon("circle-alert", size: "sm") %>
    <p class="text-sm text-secondary">
      AI-enabled rule actions will cost money.  Be sure to filter as narrowly as possible to avoid unnecessary costs.
    </p>
  </div>
<% end %>
<div class="bg-container rounded-xl shadow-border-xs p-4">
  <% if @rules.any? %>
    <div class="bg-container-inset rounded-xl">
      <div class="flex justify-between px-4 py-2 text-xs uppercase">
        <div class="flex items-center gap-1.5 font-medium text-secondary">
          <p>Rules</p>
          <span class="text-subdued">&middot;</span>
          <p><%= @rules.count %></p>
        </div>
        <div class="flex items-center gap-1">
          <span class="text-secondary">Sort by:</span>
          <%= form_with url: rules_path, method: :get, local: true, class: "flex items-center", data: { controller: "auto-submit-form" } do |form| %>
            <%= form.select :sort_by,
                      options_for_select([["Name", "name"], ["Updated At", "updated_at"]], @sort_by),
                      {},
                      class: "min-w-[120px] bg-transparent rounded border-none cursor-pointer text-primary uppercase text-xs w-auto",
                      data: { auto_submit_form_target: "auto", autosubmit_trigger_event: "change" } %>
            <%= form.hidden_field :direction, value: @direction %>
          <% end %>
          <%= render DS::Link.new(
            href: rules_path(direction: @direction == "asc" ? "desc" : "asc", sort_by: @sort_by),
            variant: "icon",
            icon: "arrow-up-down",
            size: :sm,
            title: "Toggle sort direction"
          ) %>
        </div>
      </div>
      <div class="p-1">
        <div class="flex flex-col bg-container rounded-lg shadow-border-xs">
          <%= render partial: "rule", collection: @rules, spacer_template: "shared/ruler" %>
        </div>
      </div>
    </div>
  <% else %>
    <div class="flex justify-center items-center py-20">
      <div class="text-center flex flex-col items-center max-w-[500px]">
        <p class="text-sm text-primary font-medium mb-1">No rules yet</p>
        <p class="text-sm text-secondary mb-4">Set up rules to perform actions to your transactions and other data on every account sync.</p>
        <div class="flex items-center gap-2">
          <%= render DS::Link.new(
            text: "New rule",
            variant: "primary",
            href: new_rule_path(resource_type: "transaction"),
            icon: "plus",
            frame: :modal
          ) %>
        </div>
      </div>
    </div>
  <% end %>
</div>
